//
//  AppDelegate.m
//  Personal Passlock
//
//  Created by 杨岑 on 5/5/15.
//  Copyright (c) 2015 杨岑. All rights reserved.
//

#import "AppDelegate.h"
#import "AppiCloud.h"


@interface AppDelegate ()
@property (weak) IBOutlet NSWindow *window;
@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
    // Insert code here to tear down your application
}

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
    return NO;
}

- (IBAction)test_click:(id)sender {
    NSSavePanel* savePanel = [NSSavePanel savePanel];
    savePanel.canCreateDirectories = NO;
    //savePanel.allowedFileTypes = @[@"pplk"];
    
    [savePanel beginSheetModalForWindow:[self window] completionHandler:
     ^(NSInteger result) {
         if (result == NSFileHandlingPanelOKButton) {
             //NSLog(@"%@", [NSThread currentThread]);
             
             [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                 NSError* error;
                 NSURL* linkUrl = savePanel.URL;
                 NSURL* testUrl = [[AppiCloud singleton] formatDocURL:@"new.pplk"];

                 [linkUrl startAccessingSecurityScopedResource];
                 NSFileManager* fm = [[NSFileManager alloc] init];
                 //if ([fm fileExistsAtPath:linkUrl.path])
                 {
                     [fm removeItemAtURL:linkUrl error:&error];
                     if (error)
                     {
                         NSLog(@"%@", error);
                     }
                 }
                 error = nil;
                 [fm createSymbolicLinkAtURL:linkUrl withDestinationURL:testUrl error:&error];
                 if (error)
                 {
                     NSLog(@"%@", error);
                 }
                 [linkUrl stopAccessingSecurityScopedResource];
             }];
         }
     }];
    
}

@end
